package system.until;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageZipUtil {
	
	public static String zipImageFile(String oldFile,int width,int height,float quality,String smallcon){
		if(oldFile==null){
			return null;
		}
		String newImage=null;
		try{
		Image srcFile=ImageIO.read(new File(oldFile));
		File file=new File(oldFile);
		long length=file.length();
		int w=srcFile.getWidth(null);
		int h=srcFile.getHeight(null);
		if(length>500000){
			w=w/2;
			h=h/2;
		}
	
		BufferedImage tag=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
		tag.getGraphics().drawImage(srcFile, 0, 0, w, h, null);
		String filePrex=oldFile.substring(0,oldFile.indexOf('.'));
		newImage=filePrex+smallcon+oldFile.substring(filePrex.length());
		FileOutputStream out=new FileOutputStream(newImage);
		JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
		JPEGEncodeParam jep=JPEGCodec.getDefaultJPEGEncodeParam(tag);
		jep.setQuality(quality, true);
		encoder.encode(tag,jep);
		out.close();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
		 return newImage; 
	}
	
	public static String zipImageFileDif(String oldFile,int width,int height,float quality,String smallPicPath,
			String smallImagePath,String smallFilePath){
		if(oldFile==null){
			return null;
		}
		try{
		Image srcFile=ImageIO.read(new File(oldFile));
		int w=0;
		int h=0;
		if(srcFile!=null){
			w=srcFile.getWidth(null);
		    h=srcFile.getHeight(null);
			if(w>1024){
			w=w/4;
			h=h/4;
			}
		}else{
			return "pictureCanNotRead";
		}
	
		BufferedImage tag=new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
		tag.getGraphics().drawImage(srcFile, 0, 0, w, h, null);
	
		File dirFile = null;
		dirFile = new File(smallPicPath);
		if (!(dirFile.exists()) && !(dirFile.isDirectory())) {
			boolean creadoc = dirFile.mkdirs();
			if (creadoc) {
				System.out.println("OK,创建文件夹成功！"+smallPicPath);
			} else {
				System.out.println("ERROR,创建文件夹失败！"+smallPicPath);
			}
		}
		
		File dirPicFile = null;
		dirPicFile = new File(smallImagePath);
		if (!(dirPicFile.exists()) && !(dirPicFile.isDirectory())) {
			boolean creadoc = dirPicFile.mkdirs();
			if (creadoc) {
				System.out.println("OK,创建文件夹成功！"+smallImagePath);
			} else {
				System.out.println("ERROR,创建文件夹失败！"+smallImagePath);
			}
		}
		
		FileOutputStream out=new FileOutputStream(smallFilePath);
		JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
		JPEGEncodeParam jep=JPEGCodec.getDefaultJPEGEncodeParam(tag);
		jep.setQuality(quality, true);
		encoder.encode(tag,jep);
		out.close();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
		 return smallFilePath; 
	}
	public static void main(String[] args) {
		ImageZipUtil.zipImageFile("D:/4211333035071Y/100031.jpg",4096,3072,1f,"000000");
	}
}
